package com.huafv.flyauth.web.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huafv.flyauth.core.domain.Result;
import com.huafv.flyauth.web.admin.entity.SysUser;
import com.huafv.flyauth.web.admin.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/system/user")
public class SysUserController extends BaseController {


    @Autowired
    SysUserService sysUserService;

    @PreAuthorize("hasAuthority('user:list')")
    @GetMapping("/")
    public Result list(HttpServletRequest httpRequest) {
        // 定义页码和页大小
        int pageNum = ServletRequestUtils.getIntParameter(httpRequest, "pageNum", 1);
        int pageSize = ServletRequestUtils.getIntParameter(httpRequest, "pageSize", 10);

        // 查询用户的记录总数
        long total = sysUserService.count(null);

        // 创建一个Page对象，使用给定的页码和页大小
        Page<SysUser> page = new Page<>(pageNum, pageSize);

        // 使用MyBatis-Plus执行分页查询
        IPage<SysUser> userPage = sysUserService.page(page);

        // 从分页查询结果中获取用户列表
        List<SysUser> userList = userPage.getRecords();

        Map<String,Object> m = new HashMap<>();

        m.put("usersData", userList);
        m.put("total", total);


        // 将用户列表返回给前端
        return Result.success(m);
    }


}
